package com.example.demothree.service.imp;

import com.example.demothree.bean.Employee;
import com.example.demothree.bean.Managers;
import com.example.demothree.bean.UserName;
import com.example.demothree.dao.EmployeeDao;
import com.example.demothree.dao.ManagersDao;
import com.example.demothree.dao.UserNameDao;
import com.example.demothree.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class LoginServiceImp  implements LoginService {

    @Resource
    private ManagersDao managersDao;
    @Resource
    private EmployeeDao employeeDao;
    @Resource
    private UserNameDao userNameDao;
    @Resource
    private ThymeleafViewResolver thymeleafViewResolver ;
    //用户
    public Employee loginname(String username) {
        Employee loginname = employeeDao.findEmployeeByName(username);
        return loginname;
    }
    //用户id
    @Override
    public Employee findbyId(Long id) {
        return employeeDao.findBy(id);
    }
    //管理员
    @Override
    public Managers adminname(String username) {
        Managers loginDaoName = managersDao.findName(username);
        return loginDaoName;
    }
    //管理员id
    @Override
    public Managers findglybyId(Long id) {
        return managersDao.findBy(id);
    }

    //添加用户
    @Override
    public Employee addyh(Employee employee) {
        UserName userName = new UserName();
        userName.setName(employee.getName());
        userName.setEmployeeId(employee.getId());
        userNameDao.save(userName);
        return employeeDao.save(employee);
    }
    //修改用户
    @Override
    public void upyh(Employee employee,Long id) {
        employeeDao.save(employee);
//        Employee employee1 = findemployeeId(id);
//        System.out.println(id);
//        employee1.setName(employee.getName());
//        employee1.s(employee.getId());
//        userNameDao.save(employee1);


    }
    //name查询
    @Override
    public Employee findByName(String name) {
        return employeeDao.findEmployeeByName(name);
    }
    //退出方法
    @Override
    public void outLogin(HttpServletRequest request) {
        String outlogin = null;
        thymeleafViewResolver.addStaticVariable("managers",outlogin);
        thymeleafViewResolver.addStaticVariable("employee",outlogin);
        request.getSession().setAttribute("username",outlogin);
    }

    //name查询
    @Override
    public UserName findUserNameByName(String name) {
        return userNameDao.findUserNameByName(name);
    }
    //id查询用户
    @Override
    public Employee findemployeeId(Long id) {
        return employeeDao.findBy(id);
    }
    //id查询管理员
    @Override
    public UserName findmanagersId(Long id) {
        System.out.println(id+"algdee");
        return userNameDao.findmanagersId(id);
    }
    //添加管理员
    @Override
    public Managers addgly(Managers managers) {
        UserName userName = new UserName();
        userName.setName(managers.getName());
        userName.setManagersId(managers.getId());
        userNameDao.save(userName);
        return managersDao.save(managers);
    }
    //修改管理员
    @Override
    public void upgly(Managers managers,Long id) {
        managersDao.save(managers);
//        UserName userName = findmanagersId(Long.valueOf(id));
//        userName.setName(managers.getName());
//        userName.setManagersId(managers.getId());
//        userNameDao.save(userName);
    }
    //删除用户名
    @Override
    public void deleteusername(Long id) {
        userNameDao.deleteById(id);
    }
}
